package com.example.komunikator;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;


import android.widget.TextView;

import org.jivesoftware.smack.PacketListener;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.filter.MessageTypeFilter;
import org.jivesoftware.smack.filter.PacketFilter;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smack.util.StringUtils;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

/**
 * An example full-screen activity that shows and hides the system UI (i.e.
 * status bar and navigation/system bar) with user interaction.
 * 
 * @see SystemUiHider
 */
public class SingleChatWindow extends LoginActivity {
	private static final String url = "jdbc:mysql://mysql.agh.edu.pl:3306/rybczyns";
	private static final String user = "rybczyns";
	private static final String pass = "6gtZxjms";
	ArrayAdapter<String> adapter;


	String mRecipient;
	ListView mList;
	EditText mSendText;
	TextView mRecipientArea;
	private Handler mHandler = new Handler();
	public AddUserToSQL mDialog;
	//	private XMPPConnection connection;
	private ArrayList<String> messages = new ArrayList();

	public void onCreate(Bundle icicle) {
		super.onCreate(icicle);
		connection = XMPPLogic.getInstance().getConnection();
		
		setContentView(R.layout.activity_single_chat_window);
		mDialog = new AddUserToSQL(this);
		//		requestWindowFeature(Window.FEATURE_NO_TITLE);
		//		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
		//				WindowManager.LayoutParams.FLAG_FULLSCREEN);

		//		setTitle(R.string.app_name);
		//		

		mRecipient = "";
		Bundle GivedArgs = getIntent().getExtras();

		if (GivedArgs!=null){
			mRecipient = GivedArgs.getString("mRecipient");
		}else
		{
			Toast.makeText(this,"There were errors in Recipient setup!", Toast.LENGTH_LONG).show();
		}



		mSendText = (EditText) this.findViewById(R.id.sendText);
		mList = (ListView) this.findViewById(R.id.listMessages);
		mRecipientArea = (TextView) this.findViewById(R.id.recipient);
		mRecipientArea.setText(mRecipient);
		setListAdapter();


		Button send = (Button) this.findViewById(R.id.send);
		send.setOnClickListener(new View.OnClickListener() {
			public void onClick(View view) {
				String to = mRecipient;//.getText().toString();
				String text = mSendText.getText().toString();


				Log.i("XMPPClient", "Sending text [" + text + "] to [" + to
						+ "]");
				Message msg = new Message(to, Message.Type.chat);
				msg.setBody(text);
				connection.sendPacket(msg);
				messages.add(connection.getUser() + ":");
				messages.add(text);
				//
				mSendText.setText("");

				setListAdapter();
			}
		});


	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item){

		switch (item.getItemId()){
		
		case R.id.add:
			mHandler.post(new Runnable() {
				public void run() {
					mDialog.show();
				}
			});
			return true;
		
		case R.id.delete:
			Toast.makeText(this, "Delete Clicked!", Toast.LENGTH_SHORT).show();
			String temp = null;
			try {  
				
				temp = mRecipient.toString();
				Connection con = SQLLogic.getInstance().getConnection();
				Statement st = con.createStatement();  
				String query = "DELETE FROM Users WHERE mail=\""+temp+"\"";
				Log.i("SQLDebug", query);
				st.executeUpdate(query);  

				
			}catch  (Exception ex){
				ex.printStackTrace();

			}


			return true;
		case R.id.Send_Task:
			Intent i = new Intent(Intent.ACTION_SEND);
			i.setType("message/rfc822");
			
			i.putExtra(Intent.EXTRA_EMAIL  , new String[]{"bartlomiej.rybczynski@gmail.com"});
			i.putExtra(Intent.EXTRA_SUBJECT, "subject of email");
			i.putExtra(Intent.EXTRA_TEXT   , "body of email");
			try {
			    startActivity(Intent.createChooser(i, "Send mail..."));
			} catch (android.content.ActivityNotFoundException ex) {
			    Toast.makeText(SingleChatWindow.this, "There are no email clients installed.", Toast.LENGTH_SHORT).show();
			}
			

			return true;
		default: 
			return super.onOptionsItemSelected(item);
		}

	}


	@Override
	public boolean onCreateOptionsMenu(Menu menu){
		MenuInflater menuInflater = getMenuInflater();
		menuInflater.inflate(R.menu.user_options_list, menu);
		return true;

	}
	public void setConnection(XMPPConnection connection) {
		this.connection = connection;
		if (connection != null) {
			// Add a packet listener to get messages sent to us
			PacketFilter filter = new MessageTypeFilter(Message.Type.chat);
			connection.addPacketListener(new PacketListener() {
				public void processPacket(Packet packet) {
					Message message = (Message) packet;
					if (message.getBody() != null) {
						String fromName = StringUtils.parseBareAddress(message
								.getFrom());
						Log.i("XMPPClient", "Got text [" + message.getBody()
								+ "] from [" + fromName + "]");
						messages.add(fromName + ":");
						messages.add(message.getBody());
						// Add the incoming message to the list view
						mHandler.post(new Runnable() {
							public void run() {
								setListAdapter();
							}
						});
					}
				}
			}, filter);
		}
	}

	private void setListAdapter() {
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
				R.layout.single_multi_line_list_item, R.id.text1, messages);
		// messages);
		mList.setAdapter(adapter);


	}

}
